#ifndef REGINFO_H
#define REGINFO_H

/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author zhouyan
 * @brief 数据转换实现
*/
#include "regbase.h"

#define POLLING_STRING_REG_CYCLE  300

typedef struct cJSON cJSON;


class RegFloat : public RegBase {
public:
    RegFloat(const std::string &name, DXType raw_Type, float k, float b, float diff);

    virtual float toFloat(void) const {return latest_data_;}
    virtual std::string toString(void) const;

    virtual DXType rawDataType(void) const {return raw_type_;}

    virtual bool isFloat(void) const { return true; }

    virtual bool changed(bool force = false);

    virtual uint8_t setRawData(const void *buf);
    virtual void setRawData(bool val) { latest_data_ = val * k_ + b_; reg_status_ = VALID; }
    virtual void setRawData(int val)  { latest_data_ = val * k_ + b_; reg_status_ = VALID; }
    virtual void setRawData(float val){ latest_data_ = val * k_ + b_; reg_status_ = VALID; }
    virtual void setRawData(const char *str);

    virtual void setRealData(bool val) { latest_data_ = val; reg_status_ = VALID; }
    virtual void setRealData(int val)  { latest_data_ = val; reg_status_ = VALID; }
    virtual void setRealData(float val){ latest_data_ = val; reg_status_ = VALID; }
    virtual void setRealData(const char *str);

    virtual int toRawDataInt(void) const { return static_cast<int>(toRawDataFloat()); }
    virtual float toRawDataFloat(void) const;

    virtual RegBase *newReg(const std::string &name) {
        return new RegFloat(name, raw_type_, k_, b_, diff_);
    }

    virtual float k() const {return k_;}
    virtual float b() const {return b_;}
protected:
    DXType raw_type_;
    float latest_data_;
    float transfer_data_;
    float k_;
    float b_;
    float diff_;
};

class RegInt : public RegBase {
public:
    RegInt(const std::string &name, DXType raw_Type, int diff);

    virtual float toFloat(void) const {return latest_data_;}
    virtual std::string toString(void) const;
    virtual int toInt(void) const { return latest_data_; }

    virtual DXType rawDataType(void) const {return raw_type_;}
    virtual bool isInt(void) const { return true; }

    virtual bool changed(bool force = false);

    virtual uint8_t setRawData(const void *buf);
    virtual void setRawData(bool val) { latest_data_ = val; reg_status_ = VALID; }
    virtual void setRawData(int val)  { latest_data_ = val; reg_status_ = VALID; }
    virtual void setRawData(float val){ latest_data_ = static_cast<int>(val); reg_status_ = VALID; }
    virtual void setRawData(const char *str);

    virtual void setRealData(bool val) { setRawData(val); }
    virtual void setRealData(int val)  { setRawData(val); }
    virtual void setRealData(float val){ setRawData(val); }
    virtual void setRealData(const char *str) { setRawData(str); }

    virtual int toRawDataInt(void) const { return latest_data_; }
    virtual float toRawDataFloat(void) const { return latest_data_; }

    virtual RegBase *newReg(const std::string &name) {
        return new RegInt(name, raw_type_, diff_/100);
    }
    virtual float k() const {return -1;}
    virtual float b() const {return -1;}

protected:
    DXType raw_type_;
    int latest_data_;
    int transfer_data_;
    int diff_;
};

class RegUint : public RegBase {
public:
    RegUint(const std::string &name, DXType raw_Type, uint32_t diff);

    virtual float toFloat(void) const {return latest_data_;}
    virtual std::string toString(void) const;
    virtual uint32_t toUint(void) const { return latest_data_; }
    virtual int32_t toInt(void) const { return static_cast<int32_t>(latest_data_); }

    virtual DXType rawDataType(void) const {return raw_type_;}
    virtual bool isUint(void) const { return true; }

    virtual bool changed(bool force = false);

    virtual uint8_t setRawData(const void *buf);
    virtual void setRawData(bool val) { latest_data_ = val; reg_status_ = VALID; }
    virtual void setRawData(int val)  { latest_data_ = static_cast<uint32_t>(val); reg_status_ = VALID; }
    virtual void setRawData(float val){ latest_data_ = static_cast<uint32_t>(val); reg_status_ = VALID; }
    virtual void setRawData(const char *str);

    virtual void setRealData(bool val) { setRawData(val); }
    virtual void setRealData(int val)  { setRawData(val); }
    virtual void setRealData(float val){ setRawData(val); }
    virtual void setRealData(const char *str) { setRawData(str); }

    virtual int toRawDataInt(void) const { return  static_cast<int>(latest_data_); }
    virtual float toRawDataFloat(void) const { return latest_data_; }

    virtual RegBase *newReg(const std::string &name) {
        return new RegUint(name, raw_type_, diff_/100);
    }
    virtual float k() const {return -1;}
    virtual float b() const {return -1;}

protected:
    DXType raw_type_;
    uint32_t latest_data_;
    uint32_t transfer_data_;
    uint32_t diff_;
};

class RegBit : public RegBase {
public:
    RegBit(const std::string &name, bool reverse=false);

    virtual float toFloat(void) const {return latest_status_;}
    virtual std::string toString(void) const;
    virtual int toInt(void) const { return latest_status_; }

    virtual DXType rawDataType(void) const {return TYPE_BIT1;}
    virtual bool isBool(void) const { return true; }

    virtual bool changed(bool force = false);

    virtual uint8_t setRawData(const void *buf);
    virtual void setRawData(bool val);
    virtual void setRawData(int val);
    virtual void setRawData(float val);
    virtual void setRawData(const char *str);

    virtual void setRealData(bool val) { setRawData(val); }
    virtual void setRealData(int val)  { setRawData(val); }
    virtual void setRealData(float val){ setRawData(val); }
    virtual void setRealData(const char *str) { setRawData(str); }

    virtual int toRawDataInt(void) const { return  latest_status_; }
    virtual float toRawDataFloat(void) const { return latest_status_; }

    virtual RegBase *newReg(const std::string &name) {
        return new RegBit(name, reverse_);
    }
    virtual float k() const {return -1;}
    virtual float b() const {return -1;}

protected:
    bool latest_status_;
    bool transfer_status_;
    bool reverse_;
};

class RegString : public RegBase {
public:
    RegString(const std::string &name, const std::string & data=std::string());

    virtual float toFloat(void) const {return 0;}
    virtual std::string toString(void) const { return latest_data_; }
    virtual DXType rawDataType(void) const { return TYPE_STR;}

    virtual bool changed(bool force = false);

    virtual uint8_t setRawData(const void *buf);
    virtual void setRawData(bool val) {setRawData(static_cast<int>(val));}
    virtual void setRawData(int val);
    virtual void setRawData(float val);
    virtual void setRawData(const char *str);

    virtual void setRealData(bool val) { setRawData(val); }
    virtual void setRealData(int val)  { setRawData(val); }
    virtual void setRealData(float val){ setRawData(val); }
    virtual void setRealData(const char *str) { setRawData(str); }

    virtual int toRawDataInt(void) const { return  0; }
    virtual float toRawDataFloat(void) const { return 0.0f; }

    virtual RegBase *newReg(const std::string &name) {
        return new RegString(name);
    }
    virtual float k() const {return -1;}
    virtual float b() const {return -1;}

protected:
    std::string latest_data_;
    std::string transfer_data_;
};

void dumpRegData(const RegInfos &regs);
void dumpRegData(const RegBase* reg);

int parseRegs(cJSON *root, RegInfos &regs);

#endif // REGINFO_H
